package cn.duli.xianer.controller;


import cn.duli.xianer.pojo.GoodsCollection;
import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.service.GoodsCollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/goodsCollection")
public class GoodsCollectionController {

    @Resource
    private GoodsCollectionService goodsCollectionService;

    /**
     * 新增收藏
     * @param goodsCollection
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult save(GoodsCollection goodsCollection) {
        goodsCollectionService.save(goodsCollection);
        return new MyResult(200, "新增收藏",null,null,null,null);
    }

    /**
     * 根据商品id和用户id查看该商品的收藏信息
     * @param gcGid
     * @param gcUid
     * @return
     */
    @GetMapping("/findByAllId")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findByAllId(@RequestParam("gcGid") Integer gcGid,
                             @RequestParam("gcUid") Integer gcUid) {
        GoodsCollection goodsCollection = goodsCollectionService.findByAllId(gcGid, gcUid);
        return new MyResult(200, "根据id查询收藏详情",null,null,goodsCollection,null);
    }

    /**
     * 根据id查询收藏详情
     * @param gcId
     * @return
     */
    @GetMapping("/findById/{gcId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findById(@PathVariable("gcId") Integer gcId) {
        GoodsCollection goodsCollection = goodsCollectionService.findById(gcId);
        return new MyResult(200, "根据id查询收藏详情",null,null,goodsCollection,null);
    }

    /**
     * 根据id删除（取消）此收藏
     * @param gcId
     */
    @RequestMapping("/deleteById/{gcId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult deleteById(@PathVariable("gcId") Integer gcId) {
        goodsCollectionService.deleteById(gcId);
        return new MyResult(200, "根据id删除（取消）此收藏",null,null,null,null);
    }

    /**
     * 根据用户id查看该用户收藏的商品列表
     * @param gcUid
     * @return
     */
    @RequestMapping("/findByUserId")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findByUserId(@RequestParam("gcUid") Integer gcUid,
                                 @RequestParam(defaultValue = "1") int pageNo,
                                 @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<GoodsCollection> pageInfo = new PageInfo<>(goodsCollectionService.findByUserId(gcUid));
        return new MyResult(200, "根据用户id查看该用户收藏的商品列表",null,null,null,pageInfo);
    }

    /**
     * 根据商品id和用户id查看该商品是否被收藏
     * @param gcGid
     * @param gcUid
     * @return
     */
    @RequestMapping("/isCollection")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult isCollection(Integer gcGid, Integer gcUid) {
        boolean result = goodsCollectionService.isCollection(gcGid, gcUid);
        return new MyResult(200, "根据商品id和用户id查看该商品是否被收藏",null,null,result,null);
    }

    /**
     * 分页查询所有收藏商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                                   @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<GoodsCollection> pageInfo = new PageInfo<>(goodsCollectionService.findAll());
        return new MyResult(200, "分页查询所有收藏商品列表",null,null,null,pageInfo);
    }
}
